const fs = require("fs");
const uuid = require("uuid");

module.exports = {
    mkdir(local) {
        return new Promise((resolve) => {
            fs.access(local, (err) => {
                if (err) {
                    fs.mkdir(local, { recursive: true }, (err) => {
                        if (err) {
                            throw err;
                        }
                        resolve(true);
                    });
                }
                resolve(true);
            });
        });
    },
    async unlink(local) {
        try {
            await fs.promises.access(local);
            await fs.promises.unlink(local);
            console.log("文件删除成功");
            return true;
        } catch (err) {
            if (err.code === "ENOENT") {
                console.log("文件不存在");
                return { msg: "文件不存在" };
            }
            throw err;
        }
    },
    getUid() {
        return uuid.v4();
    },
    rename(filename) {
        let nameArr = filename.split(".");
        return Math.random().toString(32).split(".")[1] + "." + nameArr[1];
    },
};
